/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package SynchronizationLayer;

import Repository.IRepositoryProxy;
import java.net.InetAddress;
import java.util.ArrayList;

/**
 *
 * @author Tomasz Nowak
 */
public class ConversationRunner implements Runnable
{
	NotarySynchronizationPacket receivedPacket;

	IRepositoryProxy repositoryProxy;
	ISynchronizationServer synchronizationServer;
	ArrayList<ExpectedPacket> expectedPackets;
	IPacketHandler packetHandler;

	public ConversationRunner( IRepositoryProxy repositoryProxy, ISynchronizationServer synchronizationServer, ArrayList<ExpectedPacket> expectedPackets, IPacketHandler packetHandler )
	{
		this(null, repositoryProxy, synchronizationServer, expectedPackets, packetHandler);
	}

	public ConversationRunner( NotarySynchronizationPacket receivedPacket, IRepositoryProxy repositoryProxy, ISynchronizationServer synchronizationServer, ArrayList<ExpectedPacket> expectedPackets, IPacketHandler packetHandler )
	{

		this.receivedPacket = receivedPacket;
		this.repositoryProxy = repositoryProxy;
		this.synchronizationServer = synchronizationServer;
		this.expectedPackets = expectedPackets;
		this.packetHandler = packetHandler;
	}

	@Override
	public void run()
	{
		PacketHandlerResult result = packetHandler.handle(receivedPacket, repositoryProxy,
																											synchronizationServer);

		packetHandler.setExpectedPackets(result, expectedPackets);
	}

	void handleTimeout()
	{
		//todo: trzeba podawac typ pakietu ( albo cos innego ), aby wiedziec, jak handlowac timeout
		packetHandler.handleTimeout(repositoryProxy, synchronizationServer);
	}
}
